/*
 * Copyright (c) 2018.
 * This file was written by Fancyears·Milos·Malvis @connor. Any question/bug you can't post to 1243093366@qq.com.
 * ALL RIGHTS RESERVED.
 */
package org.fancyears.intercepts;

import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

/**
 * @author: Fancyears·Milos·Malvis
 * @CreateDate: 2019年1月10日 下午4:18:55
 * @Description:这是一个实例拦截器
 */
/**
 * 
 * MyBatis 允许使用插件来拦截的方法调用包括：
 * 
 * ·Executor (update, query, flushStatements, commit,rollback, getTransaction,
 * close, isClosed)
 * 
 * ·ParameterHandler (getParameterObject, setParameters)
 * 
 * ·ResultSetHandler (handleResultSets,handleOutputParameters)
 * 
 * ·StatementHandler (prepare, parameterize, batch, update,query)
 */
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class ExamplePlugin implements Interceptor {
	/**
	 * 此处可以写处理代码
	 */
	public Object intercept(Invocation invocation) throws Throwable {
		System.out.println("执行拦截器" + ExamplePlugin.class.getCanonicalName() + "intercept方法");
		return invocation.proceed();
	}

	/**
	 * 我们的插件一般都是直接用Plugin的wrap方法来生成代理对象。
	 */
	public Object plugin(Object target) {
		System.out.println("执行拦截器" + ExamplePlugin.class.getCanonicalName() + "plugin方法");
		return Plugin.wrap(target, this);
	}

	public void setProperties(Properties properties) {
		System.out.println("执行拦截器" + ExamplePlugin.class.getCanonicalName() + "setProperties方法");
	}
}